package org.paybiz.core;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.paybiz.constant.PayConstant;
import org.paybiz.core.service.pay.PayService;
import org.paybiz.model.PayModel;
import org.paybiz.model.PayReqResultDO;
import org.springframework.util.Assert;

/**
 * 支付
 * 
 * @author nassir.wen
 * @date 2013-1-7下午3:58:25
 * @company: MSD.
 * @copyright: Copyright (c) 2013
 */
public class PayHandler {

	private static final Log logger = LogFactory.getLog(PayHandler.class);

	private Map<String, PayService> payServiceMap;

	/**
	 * 支付
	 * 
	 * @author nassir.wen
	 * @date 2013-1-7下午3:55:29
	 * @return
	 */
	public PayReqResultDO pay(PayModel payDO) {
		if (logger.isInfoEnabled()) {
			logger.info(PayConstant.getPayTypeValue(payDO.getPayType())
					+ " 支付请求输入参数: " + payDO);
		}
		PayReqResultDO payReqResult = getTargetService(payDO).pay(payDO);
		if (logger.isInfoEnabled()) {
			logger.info("支付请求返回参数: " + payReqResult);
		}
		return payReqResult;
	}

	/**
	 * 获取到执行service
	 * 
	 * @author nassir.wen
	 * @date 2013-1-8上午10:55:41
	 * @param request
	 * @return
	 */
	private PayService getTargetService(PayModel payDO) {
		String payType = payDO.getPayType();
		Assert.notNull(payType, "pay type not found");
		return payServiceMap.get(payType);
	}

	public void setPayServiceMap(Map<String, PayService> payServiceMap) {
		this.payServiceMap = payServiceMap;
	}

	public Map<String, PayService> getPayServiceMap() {
		return payServiceMap;
	}

}
